package com.zadetk.helpyou.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadetk.helpyou.App;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.base.AbsActivity;
import com.zadetk.helpyou.bean.UserLoginBean;
import com.zadetk.helpyou.net.BaseObserver;
import com.zadetk.helpyou.net.BaseResponse;
import com.zadetk.helpyou.net.NetTool;
import com.zadetk.helpyou.other.Const;
import com.zadetk.helpyou.other.UserInfoManger;
import com.zadetk.helpyou.utils.LogUtil;
import com.zadetk.helpyou.utils.MD5;
import com.zadetk.helpyou.utils.SPUtil;
import com.zadetk.helpyou.utils.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.zadetk.helpyou.other.Const.USER_IS_LOGIN;

/**
 * 2-1 登录页面
 */
public class LoginActivity extends AbsActivity implements View.OnClickListener {
    private EditText etPhonenumber;
    private EditText etPassword;
    private TextView tvBtnLogin;
    private TextView tvForgetpassword;
    private TextView tvRegsister;
    private ImageView ivBack;


    private void initViews() {
        setTopTitle("登录");
        etPhonenumber = findViewById(R.id.et_phonenumber);
        etPassword = findViewById(R.id.et_password);
        tvBtnLogin = findViewById(R.id.tv_btn_login);
        tvForgetpassword = findViewById(R.id.tv_forgetpassword);
        tvRegsister = findViewById(R.id.tv_regsister);
        ivBack = findViewById(R.id.iv_back);

        //TODO 测试代码
//        etPhonenumber.setText("18642036697");
        etPhonenumber.setText("15940070932");
        etPassword.setText("123456");
//        etPassword.setText("456789");
    }

    @Override
    public int setView() {
        return R.layout.activity_login;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        initLogic();
    }

    private void initLogic() {
        tvBtnLogin.setOnClickListener(this);
        tvForgetpassword.setOnClickListener(this);
        tvRegsister.setOnClickListener(this);
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(LoginActivity.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                finish();
                break;
            case R.id.tv_btn_login:
                login();
                break;
            case R.id.tv_forgetpassword:
                startActivity(new Intent(this, ForgetPasswordActivity.class));
                break;
            case R.id.tv_regsister:
                startActivity(new Intent(this, RegsisterActivity.class));
                break;
        }
    }

    private void login() {
        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(LoginActivity.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
        String password = etPassword.getText().toString();
        String username = etPhonenumber.getText().toString();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            ToastUtil.showToast("请输入完整信息！");
            return;
        }
        Map<String, Object> param = NetTool.newParams();
        param.put("username", username);
        param.put("password", MD5.getStringMD5(password));
        NetTool.getApi().login(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<UserLoginBean>>() {
                    @Override
                    public void onData(BaseResponse<UserLoginBean> value) {
                        if (value.getCode() == 0) {
                            UserLoginBean user = value.getData();
                            SPUtil.saveDate(LoginActivity.this, Const.USER_TOKEN, user.getToken());
                            SPUtil.saveDate(LoginActivity.this, Const.USER_NAME, user.getUsername());
                            SPUtil.saveDate(LoginActivity.this, Const.USER_UID, user.getUid());
                            SPUtil.saveDate(LoginActivity.this, USER_IS_LOGIN, true);
                            UserInfoManger.isLogin = true;
                            App.finishAllActivity();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        } else {
                            LogUtil.e("登陆失败 " + value.getMessage());
                            ToastUtil.showToast("登陆失败 " + value.getMessage());
                        }
                    }
                });
    }
}

